package com.jmpt.fangbohui.http;

import android.content.Context;
import android.text.TextUtils;

import com.jmpt.fangbohui.bean.LoginBean;
import com.jmpt.fangbohui.utils.UIUtils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;


public class ApiRequest {
    public static RequestParams getBaseParams() {
        RequestParams params = new RequestParams();
        params.put("os", "android");
        return params;
    }

    public static RequestParams getBaseLoginParams(Context context) {
        RequestParams params = getBaseParams();
//        UserBean user = AccountHelper.getUser();
//        if (user != null && TextUtils.isEmpty(user.token) == false) {
//            params.put("token", user.token);
//            DebugLog.i("token:" + user.token);
//        }
        return params;
    }

    public static RequestHandle register(Context context, String username, String password, String question1, String answer1, String question2, String answer2, String pushUserId, JsonHttpHandler handler) {
        RequestParams params = getBaseLoginParams(context);
        params.put("username", username);
        params.put("password", password);
        params.put("question1", question1);
        params.put("answer1", answer1);
        params.put("question2", question2);
        params.put("answer2", answer2);
        if (TextUtils.isEmpty(pushUserId) == false) {
            params.put("push_id", pushUserId);
        }
        return AsyncHttp.post(context, ApiUrl.getAbsoluteUrl(ApiUrl.REGISTER), params, handler);
    }

    public static RequestHandle login(Context context, String username, String password, String pushUserId, JsonHttpHandler handler) {
        RequestParams params = getBaseLoginParams(context);
        params.put("username", username);
        params.put("password", password);
        if (TextUtils.isEmpty(pushUserId) == false) {
            params.put("push_id", pushUserId);
        }
        return AsyncHttp.post(context, ApiUrl.getAbsoluteUrl(ApiUrl.LOGINT), params, handler);
    }
    public static RequestHandle userLogin(Context context, String username, String password, JsonHttpHandler handler) {
        RequestParams params = getBaseLoginParams(context);
        params.put("phone", username);
        params.put("password", password);
        return AsyncHttp.post(context, ApiUrl.getAbsoluteUrl(ApiUrl.USERLOGIN), params, handler);
    }

    public static RequestHandle changePassword(Context context, String oldPassword, String password, JsonHttpHandler handler) {
        RequestParams params = getBaseLoginParams(context);
        params.put("old_pwd", oldPassword);
        params.put("new_pwd", password);
        return AsyncHttp.post(context, ApiUrl.getAbsoluteUrl(ApiUrl.CHANGE_PASSWORD), params, handler);
    }

    public static RequestHandle logout(Context context, String pushUserId, JsonHttpHandler handler) {
        RequestParams params = getBaseLoginParams(context);
        if (TextUtils.isEmpty(pushUserId) == false) {
            params.put("push_id", pushUserId);
        }
        return AsyncHttp.post(context, ApiUrl.getAbsoluteUrl(ApiUrl.LOGOUT), params, handler);
    }

    public static RequestHandle getUserInfo(Context context, JsonHttpHandler handler) {
        RequestParams params = getBaseLoginParams(context);
        return AsyncHttp.get(context, ApiUrl.getAbsoluteUrl(ApiUrl.USER_INFO), params, handler);
    }

	public static RequestHandle getRecommendsData(Context context, JsonHttpHandler handler) {
		// TODO 向服务器获取推荐信息
		return null;
	}

	public static RequestHandle getPromotesData(Context context, JsonHttpHandler handler) {
		// TODO Auto-generated method stub
		return null;
	}

	public static RequestHandle getBannerData(Context context, JsonHttpHandler handler) {
		RequestParams params = getBaseLoginParams(context);
		return AsyncHttp.get(context, ApiUrl.getAbsoluteUrl(ApiUrl.HOME_BANNER), params, handler);
	}
	/**
	 * 获取新房列表数据
	 * @param context
	 * @param zoneId  
	 * 				区域id
	 * @param PriceID
	 * 				价格范围
	 * @param orderType
	 * 				排序方式
	 * @param page
	 * 				页码
	 * @param limit
	 * 				没有数据量
	 * @param handler
	 * @return
	 */
	public static RequestHandle getNewHouseListData(Context context,String searchKey, int zoneId,
			String price, String orderType, int page, int limit,
			JsonHttpHandler handler) {
		RequestParams params = getBaseParams();
		if(!TextUtils.isEmpty(searchKey)){
			params.put("keys", searchKey);
		}
		
		if(zoneId > 0){
			params.put("zoneid", zoneId);
		}
		if(!TextUtils.isEmpty(price)){
			params.put("price", price);
		}
		if(!TextUtils.isEmpty(orderType)){  //orderType = "orderSUP=asc"|"orderTime=asc"
			String[] split = orderType.split("=");
			if(split != null && split.length == 2){
				params.put(split[0], split[1]);
			}
		}
		if(page > 0){
			params.put("page", page);
		}
//		if(limit > 0){
//			params.put("limit", limit);
//		}
		return AsyncHttp.get(context, ApiUrl.getAbsoluteUrl(ApiUrl.NEW_HOUSE_LIST), params, handler);
	}
	/**
	 * 获取新房列表数据
	 * @param context
	 * @param orderType
	 * 				排序方式
	 * @param page
	 * 				页码
	 * @param H_SaleOrRent
	 * 				售或租
	 * @param uid
	 * 				经纪人id
	 * @param handler
	 * @return
	 */
	public static RequestHandle getBrokerHouseListData(Context context, int saleOrRent, String uid, String searchKey, String orderType, int page, JsonHttpHandler handler) {
		RequestParams params = getBaseParams();
		
		params.put("H_SaleOrRent", saleOrRent);
		params.put("uid", uid);
		
		if(!TextUtils.isEmpty(searchKey)){
			params.put("keys", searchKey);
		}
		if(!TextUtils.isEmpty(orderType)){  //orderType = "orderSUP=asc"|"orderTime=asc"
			String[] split = orderType.split("=");
			if(split != null && split.length == 2){
				params.put(split[0], split[1]);
			}
		}
		if(page > 0){
			params.put("page", page);
		}
		
//		if(limit > 0){
//			params.put("limit", limit);
//		}
		return AsyncHttp.get(context, ApiUrl.getAbsoluteUrl(ApiUrl.BROKER_SHOP_ESLIST), params, handler);
	}

	public static RequestHandle getOldHouseListData(Context context, String searchKey, int zoneId,
			String totalPrice, String proportionValue,String moreUrl, int page, int limit,JsonHttpHandler handler) {
		RequestParams params = getBaseParams();
		if(!TextUtils.isEmpty(searchKey)){
			params.put("keys", searchKey);
		}
		if(zoneId > 0){
			params.put("ZoneID", zoneId);
		}
		if(!TextUtils.isEmpty(totalPrice)){
			params.put("price", totalPrice);
		}
		if(!TextUtils.isEmpty(proportionValue)){
			params.put("H_MJ", proportionValue);
		}
		
		if(!TextUtils.isEmpty(moreUrl)){  //orderTime=asc&LXID=1
			String[] split = moreUrl.split("&");
			for (String string : split) {
				String[] split2 = string.split("=");
				try {
					params.put(split2[0], split2[1]);
				} catch (Exception e) {
					e.printStackTrace();
					continue;
				}
			}
		}
		
		if(page > 0){
			params.put("page", page);
		}
//		if(limit > 0){
//			params.put("limit", limit);
//		}
		return AsyncHttp.get(context, ApiUrl.getAbsoluteUrl(ApiUrl.OLD_HOUSE_LIST), params, handler);
	}

	public static RequestHandle getRentHouseListData(Context context, String searchKey, int zoneId,
			String totalPrice, String proportionValue,String moreUrl, int page, int limit,JsonHttpHandler handler) {
		RequestParams params = getBaseParams();
		if(!TextUtils.isEmpty(searchKey)){
			params.put("keys", searchKey);
		}
		if(zoneId > 0){
			params.put("ZoneID", zoneId);
		}
		if(!TextUtils.isEmpty(totalPrice)){
			params.put("price", totalPrice);
		}
		if(!TextUtils.isEmpty(proportionValue)){
			params.put("H_MJ", proportionValue);
		}
		if(!TextUtils.isEmpty(moreUrl)){  //orderTime=asc&LXID=1
			String[] split = moreUrl.split("&");
			for (String string : split) {
				String[] split2 = string.split("=");
				try {
					params.put(split2[0], split2[1]);
				} catch (Exception e) {
					e.printStackTrace();
					continue;
				}
			}
		}
		if(page > 0){
			params.put("page", page);
		}
//		if(limit > 0){
//			params.put("limit", limit);
//		}
		
		return AsyncHttp.get(context, ApiUrl.getAbsoluteUrl(ApiUrl.RENT_HOUSE_LIST), params, handler);
	}
	
	/**
	 * 获取 经纪人 列表
	 * @param context
	 * @param ZoneId
	 * @param companyId
	 * @param searchKey
	 * @param page
	 * @param handler
	 * @return
	 */
	public static RequestHandle getBrokerListData(Context context, String ZoneId, String companyId, String searchKey, int page, JsonHttpHandler handler){
		
		RequestParams params = getBaseParams();
		if(page > 0){
			params.put("page", page);
		}
		if(!TextUtils.isEmpty(ZoneId)){
			params.put("zoneid", ZoneId);
		}
		if(!TextUtils.isEmpty(companyId)){
			params.put("zjid", companyId);
		}
		if(!TextUtils.isEmpty(searchKey)){
			params.put("keys", searchKey);
		}
		
		return AsyncHttp.get(context, ApiUrl.getAbsoluteUrl(ApiUrl.BROKER_LIST), params, handler);
	}
	
	public static RequestHandle getBrokerCompanyList(String zoneId, JsonHttpHandler handler){
		RequestParams params = getBaseParams();
		params.put("ZoneID", zoneId);
		return AsyncHttp.get(UIUtils.getContext(), ApiUrl.AGENT_ZJ_COMPANY, params, handler);
	}
	
	public static RequestHandle getBrokerShopDetail(String uid, JsonHttpHandler handler){
		//http://www.cnfbh.com/apptest/broker_shop.php?uid=1812
		RequestParams params = getBaseParams();
		params.put("uid", uid);
		return AsyncHttp.get(UIUtils.getContext(), ApiUrl.getAbsoluteUrl(ApiUrl.BROKER_SHOP), params, handler);
	}
	
	public static RequestHandle getNewHouseDetail(int houseId, JsonHttpHandler handler){
		//http://www.cnfbh.com/apptest/houseView.php?id=1
		RequestParams params = getBaseParams();
		params.put("id", houseId);
		return AsyncHttp.get(UIUtils.getContext(), ApiUrl.getAbsoluteUrl(ApiUrl.NEW_HOUSE_DETAIL), params, handler);
	}
	
	public static RequestHandle getOldHouseDetail(int houseId, JsonHttpHandler handler){
		//http://www.cnfbh.com/apptest/houseView.php?id=1
		RequestParams params = getBaseParams();
		params.put("HouseID", houseId);
		return AsyncHttp.get(UIUtils.getContext(), ApiUrl.getAbsoluteUrl(ApiUrl.OLD_HOUSE_DETAIL), params, handler);
	}

	public static RequestHandle getRentHouseFilter(int cityId,
			JsonHttpHandler handler) {
		RequestParams params = getBaseParams();
		params.put("CityID", cityId);
		String url = "http://www.cnfbh.com/App/Ho_House/filterConditionRent";
		return AsyncHttp.get(UIUtils.getContext(), url, params, handler);
	}
	
	public static RequestHandle getOldHouseFilter(int cityId,
			JsonHttpHandler handler) {
		RequestParams params = getBaseParams();
		params.put("CityID", cityId);
		String url = "http://www.cnfbh.com/App/Ho_House/filterConditionSale";
		return AsyncHttp.get(UIUtils.getContext(), url, params, handler);
	}
	
	public static RequestHandle getNewHouseFilter(int cityId,
			JsonHttpHandler handler) {
		RequestParams params = getBaseParams();
		params.put("CityID", cityId);
		String url = "http://www.cnfbh.com/App/NewHouse/filterCondition";
		return AsyncHttp.get(UIUtils.getContext(), url, params, handler);
	}
	
	public static RequestHandle getAgentListFilter(int cityId, JsonHttpHandler handler) {
		RequestParams params = getBaseParams();
		params.put("CityID", cityId);
		String url = ApiUrl.AGENT_LIST_FILTER;
		return AsyncHttp.get(UIUtils.getContext(), url, params, handler);
	}
	
	/**
	 * 预约看房
	 * @param action
	 * @param user
	 * @param HouseID
	 * @param yy_time
	 * @param handler
	 * @return
	 */
	public static RequestHandle personAppoint(String action, LoginBean user, String HouseID, String yy_time, JsonHttpHandler handler) {
		RequestParams params = getBaseParams();
		params.put("id", user.getRetval().getUserId());
		params.put("name", user.getRetval().getUserName());
		//params.put("action", action); //action=yuyue | esyuyue
		params.put("HouseID", HouseID);
		params.put("yy_time", yy_time);
		String url = ApiUrl.getAbsoluteUrl(ApiUrl.PERSON_APPOINT + "?action=" + action);
		AsyncHttpClient client = AsyncHttp.getClient();
		client.addHeader("Cookie", "PHPSESSID=" + user.getRetval().getSessionid());
		return client.post(UIUtils.getContext(), url, params, handler);
	}
	
	public static RequestHandle getBrokerOrderData(LoginBean user, String seachKey, JsonHttpHandler handler) {
		RequestParams params = getBaseParams();
		params.put("AgentId", user.getRetval().getAgentId());
		params.put("k", seachKey);
		AsyncHttpClient client = AsyncHttp.getClient();
		client.addHeader("Cookie", "PHPSESSID=" + user.getRetval().getSessionid());
		return client.post(UIUtils.getContext(),  ApiUrl.getAbsoluteUrl(ApiUrl.OLD_HOUSE_ORDER), params, handler);
	}
	
	
}
